package com.guli.edu.controller;


import com.guli.common.result.R;
import com.guli.edu.entity.EduChapter;
import com.guli.edu.entity.dto.ChapterDto;
import com.guli.edu.service.EduChapterService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author Daniel
 * @since 2019-03-29
 */
@RestController
@RequestMapping("/eduservice/chapter")
@CrossOrigin
public class EduChapterController {

    @Autowired
    private EduChapterService eduChapterService;

    @ApiOperation(value = "添加章节的方法")
    @PostMapping
    public R addChapter(@RequestBody EduChapter eduChapter) {

        boolean flag = eduChapterService.save(eduChapter);
        if (flag) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    @ApiOperation(value = "根据id查询 ")
    @GetMapping("chapterInfo/{chapterId}")
    public R getChapterInfoById(@PathVariable String chapterId) {
        EduChapter eduChapter = eduChapterService.getById(chapterId);

        return R.ok().data("chapterData", eduChapter);
    }

    @ApiOperation(value = "修改章节信息")
    @PostMapping("updateChapter/{chapterId}")
    public R updateChapter(@PathVariable String chapterId, @RequestBody EduChapter chapter) {
        chapter.setId(chapterId);
        boolean flag = eduChapterService.updateById(chapter);
        if (flag) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    @ApiOperation(value="删除章节信息的方法")
    @DeleteMapping("{chapterId}")
    public R deleteChapterId(@PathVariable String chapterId){
        boolean flag  = eduChapterService.removeChapterId(chapterId);
        if (flag) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    @ApiOperation(value = "获取所有的章节每个章节里面的小节")
    @GetMapping("{courseId}")
    public R getChapterVideo(@PathVariable String courseId) {

        List<ChapterDto> list = eduChapterService.getAllChapterVideo(courseId);

        return R.ok().data("items", list);
    }

}

